<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="shortcut icon" href="//s01.mifile.cn/favicon.ico" type="image/x-icon">
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .adBox {
            width: 300px;
            height: 400px;
            background-color: #6ff;
            position: fixed;
            right: 5px;
            bottom: 5px;
            display: none;
        }

        .adBox .close {
            position: absolute;
            right: 0;
            top: 0;
            background-color: red;
            color: #fff;
            padding: 2px;
            user-select: none;
        }
    </style>
</head>

<body>
    <div class="adBox">
        <div class="tips">距离广告关闭还有10秒</div>
        <div class="close">X</div>
    </div>
</body>
<script>
    var adBox = document.getElementsByClassName("adBox")[0];
    var tips = document.getElementsByClassName("tips")[0];
    var closeBtn = document.getElementsByClassName("close")[0];

    var delayTimer = null;
    var timer = null;
    closeBtn.onclick = function(){
        adBox.style.display = "none";
        clearTimeout(delayTimer);
        clearInterval(timer);
    }
    loop(2000);
    function loop(delayTime){
        delayTimer = setTimeout(function() {
            adBox.style.display = "block";
            var num = 10;
            tips.innerText = `距离广告关闭还有${num}秒`;
            timer = setInterval(function() {
                num--;
                tips.innerText = `距离广告关闭还有${num}秒`;
                if(num==0){
                    clearInterval(timer);
                    adBox.style.display = "none";
                    loop(delayTime)
                }
            }, 1000);
        }, delayTime);
    }
</script>

</html>